/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Place;
import com.aroundj2me.utility.ImageLoader;

/**
 * View for displaying a list of places
 * 
 */
public class PlaceListView implements CommandListener, IView {
	private ApplicationController controller;
	private List placeList;
	// TODO could pose memory problems if many places...think of retrieving
	// partially (<20, between 20 and 40,...)
	private Vector places; // holds all the place-objects
	private final int CHARSBREAK = 30;

	public PlaceListView() {
		this.placeList = new List("Places", List.IMPLICIT);
		this.placeList.setCommandListener(this);
		createCommands();
	}

	public PlaceListView(ApplicationController parent) {
		this();
		this.controller = parent;
	}

	/**
	 * Creates the commands
	 * 
	 */
	private void createCommands() {

	}

	/**
	 * loads a list of places on the UI
	 * 
	 */
	public void loadList() {
		if (places != null) {
			for (int i = 0; i < places.size(); i++) {
				Place tmp = (Place) places.elementAt(i);
				String name = tmp.getLandmark().getName();
				if (name.length() > CHARSBREAK)
					name = name.substring(0, CHARSBREAK) + "...";

				String placeCategory = null;
				String[] categories = tmp.getCategories();
				if (categories != null
						&& categories.length > 0)
					placeCategory = categories[0];

				placeList.append(name, ImageLoader
						.getCategoryImage(placeCategory));
			}
		}
	}

	/**
	 * Returns the selected place of the list
	 * 
	 * @return selected {@link Place} object
	 */
	public Place getSelectedPlace() {
		return (Place) places.elementAt(placeList.getSelectedIndex());
	}

	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.BACK) {
			ScreenController.getInstance().last();
		} else if (c.getCommandType() == 1 && d == placeList) {
			int index = placeList.getSelectedIndex();
			Place p = (Place) places.elementAt(index);
			controller.displayPlace(p);
		}
	}

	/**
	 * Removes the selected Place-entry from the list
	 * 
	 */
	public void removeSelectedEntry() {
		int index = placeList.getSelectedIndex();
		placeList.delete(index);
		places.removeElementAt(index);
	}

	/**
	 * Resets the object in order to reuse them without having the need to
	 * instantiate a new one
	 * 
	 */
	public void reset() {
		placeList.deleteAll();
	}

	/*
	 * Getters and Setters
	 */

	public List getPlaceList() {
		return placeList;
	}

	public Vector getPlaces() {
		return places;
	}

	public void setPlaces(Vector places) {
		this.places = places;
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;
	}

}
